package com.example.file.readwrite;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Calendar;

/**
 * @Author: sn
 * @Desc something
 * @Date: 2021/9/13
 */
public class Test {


}

class Thread_readFile extends Thread{
    @Override
    public void run(){
        try {
            Calendar calstart=Calendar.getInstance();
            sleep(5000);
            File file=new File("D:\\test");

            //给该文件加锁
            RandomAccessFile fis = new RandomAccessFile(file, "rw");
            System.out.println("RandomAccessFile---read:"+fis);
            FileChannel fcin=fis.getChannel();
            System.out.println("FileChannel---read:"+fcin);
            FileLock flin=null;
            while(true){
                try {
                    flin = fcin.tryLock();
                    break;
                } catch (Exception e) {
                    System.out.println("有其他线程正在操作该文件，当前线程休眠1000毫秒-----read");
                    sleep(1000);
                }

            }
            byte[] buf = new byte[1024];
            StringBuffer sb=new StringBuffer();
            while((fis.read(buf))!=-1){
                sb.append(new String(buf,"utf-8"));
                buf = new byte[1024];
            }

            System.out.println(sb.toString());

            flin.release();
            fcin.close();
            fis.close();
            fis=null;

            Calendar calend=Calendar.getInstance();
            System.out.println("读文件共花了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"秒");
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread_writeFile extends Thread{
    @Override
    public void run(){
        Calendar calstart=Calendar.getInstance();
        File file=new File("D:\\test");
        try {
            if(!file.exists())
                file.createNewFile();

            //对该文件加锁
            RandomAccessFile out = new RandomAccessFile(file, "rw");
            System.out.println("RandomAccessFile---write:"+out);
            FileChannel fcout=out.getChannel();
            System.out.println("FileChannel---write:"+fcout);
            FileLock flout=null;
            while(true){
                try {
                    flout = fcout.tryLock();
                    break;
                } catch (Exception e) {
                    System.out.println("有其他线程正在操作该文件，当前线程休眠1000毫秒-----write");
                    sleep(1000);
                }

            }

            for(int i=1;i<=1000;i++){
                sleep(10);
                StringBuffer sb=new StringBuffer();
                sb.append("这是第"+i+"行，应该没啥错哈 \r\n");
                out.write(sb.toString().getBytes("utf-8"));
            }

            flout.release();
            fcout.close();
            out.close();
            out=null;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Calendar calend=Calendar.getInstance();
        System.out.println("写文件共花了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"秒");
    }

    public static void main(String[] args) {
        //Thread_writeFile thf3=new Thread_writeFile();
        Thread_readFile thf4=new Thread_readFile();
        //thf3.start();
        thf4.start();
        Thread_readFile thf5=new Thread_readFile();
        thf5.start();

    }
}
